package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
)

type AdminloginBody struct {
	Account  string `json:"account" from:"account"`
	Password string `json:"password" from:"password"`
}

func Adminlogin(ctx *gin.Context) {

	request := AdminloginBody{}
	ctx.ShouldBind(&request)
	username := request.Account
	pwd := request.Password
	var admin models.Adminuser
	data_source.Db.Where("username = ?", username).First(&admin)
	if admin.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "账号不存在",
		})
		return
	}
	if admin.Pwd != pwd {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "密码错误",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    admin,
		"msg":     "ok",
	})
	return
}
